

set more off 
cap log close 
clear all
set niceness 10 
cap set processor 8
set scheme s1color 
*** Paths ****
gl path1 = "/Users/Wei/Dropbox/Census"
gl path2 = "/Users/Wei/Dropbox/Fertility/Workingdata"
gl path3 = "/Users/Wei/Dropbox/Fertility/Results"
gl path4 = "/Users/Wei/Dropbox/Fertility/Figures"

log using "$path3/census_results.log",replace 

set more off

use "$path2/marr_policy", clear

ren year yr_t
gen year = year_birth +15
merge m:1 prov year using "$path2/fines_economic", keepusing(lngdp_pc lngdp secondary primary)
drop if _merge == 2 
drop _merge 
drop year
ren yr_t year

egen fine_7_15 = rowmean(fine_age7-fine_age15)
egen fine_8_15 = rowmean(fine_age8-fine_age15)
egen fine_6_20 = rowmean(fine_age6-fine_age20)
egen fine_16_22 = rowmean(fine_age6-fine_age20)

egen fine_6_18 = rowmean(fine_age6-fine_age18)
egen fine_6_22 = rowmean(fine_age6-fine_age22)
egen fine_8_20 = rowmean(fine_age8-fine_age20)
egen fine_4_20 = rowmean(fine_age4-fine_age20)
egen fine_1_20 = rowmean(fine_age1-fine_age20)

egen fine_4_22 = rowmean(fine_age4-fine_age22)

gl FINE_ALL = "fine_6_15 fine_16_20"

gl FINE_VAR_4 =  "fine_6_15 fine_16_20 fine_21_25"
gl FINE_VAR_2 = "fine_6_20 fine_21_25"
gl FINE_VAR_1 = "fine_6_20"
gl FINE_8_22 = "fine_8_22"

replace nbroth = 99 if mi(nbroth)
replace nsister = 99 if mi(nsister)

gen lnsec = ln(secondary)
gen lnpri = ln(primary)
gen lnsec_mi = mi(lnsec)
gen lnpri_mi = mi(lnpri)
egen mean_lnsec = mean(lnsec)
egen mean_lnpri = mean(lnpri)
replace lnsec = mean_lnsec if mi(lnsec)
replace lnpri = mean_lnpri if mi(lnpri)
la var fine_6_10 "Ages 6-10"
la var fine_11_15 "Ages 11-15"
la var fine_16_20 "Ages 16-20"
la var fine_21_25 "Ages 21-25"
la var fine_6_12 "Ages 6-12"
la var fine_13_19 "Ages 13-19"
la var fine_20_25 "Ages 20-25"

gen provid = prov 
merge m:1 provid using "$path2/Comp_Law", keepusing(law_*) nogen 

cap drop elig
gen elig = 1 if year_birth >= law_year - 6 
replace elig = 0 if year_birth <= law_year - 16
replace elig = (16-(law_year - year_birth ))/10 if law_year - 16<year_birth & year_birth<law_year - 6 
drop provid


gl CONTROL_1 = "women#prov##c.year_birth women#year#year_birth"
gl CONTROL_2 = "women#prov##c.year_birth women#year#year_birth women#year#prov"

gen urban_hk = hktype == 2 
gen urban_res = city_flag <= 2 if !mi(city_flag)
replace urban_res = urban_hk if  mi(urban_res)
gen early_marr = 1-late_marr
egen mean_gdp_pc = mean(lngdp_pc), by(prov)
gen high_gdp_region = mean_gdp_pc >= 5.8

egen han_p = mean(han), by(year hhid prov region)
replace high_occ = 0 if high_occ == . & !mi(work)

* Table 1 Panel a 

su senior college early_marr work high_occ age [aw = wt] if women  // Table 1 panel a 

* Table 2 & Table C5
cap erase "$path3/tab_2_c5_c7.xls"
cap erase "$path3/tab_2_c5_c7.txt"
foreach sample in  "han""!han" {
foreach x_var in  "$FINE_VAR_1" "$FINE_VAR_4"{
foreach y_var in  "senior" "college" "early_marr" "work" "high_occ" {
reghdfe `y_var' women#c.(`x_var') if `sample' [aw = wt], a($CONTROL_1) cluster(prov)
su `y_var' if `sample' & women [aw = wt]
local mean1 = r(mean)
su `y_var' if `sample' & men [aw = wt]
local mean2 = r(mean)
cap testparm 1.women#c.(`x_var')
cap local F_women = r(F)
cap local p_women = r(p)
cap testparm 0.women#c.(`x_var')
cap local F_men = r(F)
cap local p_men = r(p)
outreg2 using "$path3/tab_2_c5_c7.xls", dec(3) append adds(Women, `mean1', Men, `mean2', F-men, `F_men', P-men, `p_men', F-women, `F_women', P-women, `p_women')
}
}
}

cap erase "$path3/tab_c3_part1.xls"
cap erase "$path3/tab_c3_part1.txt"
preserve
keep if women == 1 
foreach y_var in  "senior" "college" "early_marr" "work" "high_occ" {
 
reghdfe `y_var' women#c.(fine_6_18) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part1.xls", dec(3) append 
reghdfe `y_var' women#c.(fine_6_22) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part1.xls", dec(3) append 
reghdfe `y_var' women#c.(fine_8_20) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part1.xls", dec(3) append 
reghdfe `y_var' women#c.(fine_4_20) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part1.xls", dec(3) append 
reghdfe `y_var' women#c.(fine_1_20) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part1.xls", dec(3) append 
reghdfe `y_var' women#c.(fine_4_22) if han [aw = wt], a($CONTROL_1) cluster(prov)
outreg2 using "$path3/tab_c3_part1.xls", dec(3) append 
 
}
restore


cap erase "$path3/tab_c4.xls"
cap erase "$path3/tab_c4.txt"

foreach sample in "han" "han & age <= 30" "han & age > 30"{
foreach y_var in "married_ever"   {
foreach x_var in "$FINE_VAR_1"{
reghdfe `y_var' women#c.(`x_var') if `sample'  [aw = wt], a($CONTROL_1) cluster(prov)
cap su `y_var' if `sample' & women [aw = wt]
cap local mean1 = r(mean)
cap su `y_var' if `sample' & men [aw = wt]
cap local mean2 = r(mean)
cap testparm 1.women#c.(`x_var')
cap local F_women = r(F)
cap local p_women = r(p)
cap testparm 0.women#c.(`x_var')
cap local F_men = r(F)
cap local p_men = r(p)
outreg2 using "$path3/tab_c4.xls", dec(3) append adds(Women, `mean1', Men, `mean2', F-men, `F_men', P-men, `p_men', F-women, `F_women', P-women, `p_women')
}
}
}



** Table C9, Rows A-F ***

cap drop urban rural 
gen urban = hktype == 2
gen rural = hktype == 1

cap erase  "$path3/tab_c9_a_f.xls"
cap erase  "$path3/tab_c9_a_f.txt"

preserve 
keep if women == 1 
foreach y_var in  "senior" "college"  "married_ever" "early_marr" "work" "high_occ" {
foreach sample in "!rural" "rural" {
reghdfe `y_var' women#c.fine_6_20    if han & `sample'  [aw = wt], a($CONTROL_1)  cluster(prov)
su `y_var'  if han & `sample'  [aw = wt]
outreg2 using "$path3/tab_c9_a_f.xls", dec(3) append adds(Mean, `r(mean)')

}
}

restore 
log close 

